<template>
  <div class="app-container">
    <el-dialog :visible.sync="openStatus" :show="openStatus" @open="doOpen" @close="doClose">
      <div class="filter-container">
        <el-input v-model="listQuery.name" placeholder="角色名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-input v-model="listQuery.description" placeholder="角色描述" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" @click="handleFilter">
          <svg-icon icon-class="search" /> 搜索
        </el-button>
      </div>

      <el-table
        ref="multipleTable"
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-key="getRowKey"
        @sort-change="sortChange"
      >
        <el-table-column
          type="selection"
          width="55"
          :reserve-selection="true"
        />
        <el-table-column label="角色名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色描述" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.isSelected === 1" type="primary" circle @click="handlePutDownRoles(scope.row)">
              <svg-icon icon-class="remove" />
            </el-button>
            <el-button v-if="scope.row.isSelected === 0" type="primary" circle @click="handlePickUpRoles(scope.row)">
              <svg-icon icon-class="add" />
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-dialog>
  </div>
</template>

<script>
import { selectRolePage, pickUpRoles, putDownRoles } from '@/api/role'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'RoleTable',
  components: { Pagination },
  directives: { waves },
  props: {
    uuid: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: '',
        uuid: '',
        description: '',
        ids: [],
        sort: 'asc'
      },
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      openStatus: this.visible
    }
  },
  watch: {
    uuid(val) {
      this.listQuery.uuid = val
    },
    visible(val) {
      this.openStatus = val
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      selectRolePage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getRowKey(row) {
      return row.id
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    doOpen() {
      this.getList()
    },
    doClose() {
      this.$emit('dialogData', false)// 子组件对openStatus修改后向父组件发送事件通知
    },
    closeDialogForm() {
      this.openStatus = false
    },
    handlePickUpRoles(row) {
      const roleUser = { userId: this.uuid, roleId: '' }
      roleUser.roleId = row.id
      pickUpRoles(roleUser).then(() => {
        this.getList()
        this.$notify({
          title: 'Success',
          message: '分配角色成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handlePutDownRoles(row) {
      const roleUser = { userId: this.uuid, roleId: '' }
      roleUser.roleId = row.id
      putDownRoles(roleUser).then(() => {
        this.getList()
        this.$notify({
          title: 'Success',
          message: '删除角色成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = 'asc'
      } else {
        this.listQuery.sort = 'desc'
      }
      this.handleFilter()
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}`
        ? 'ascending'
        : sort === `-${key}`
          ? 'descending'
          : ''
    }
  }
}
</script>
